package com.samen.texttools;

import android.graphics.Color;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.ForegroundColorSpan;

/**
 * Class enable colorizing text
 * 
 * @author Santosh Kumar D
 * 
 */
public class SpannedText {

	private Spannable spannable;

	public SpannedText(String str) {
		spannable = new SpannableString(str);
	}

	public Spannable get() {
		return spannable;
	}

	/**
	 * Colorizez substring with color
	 * 
	 * @param subString
	 *            -
	 * @param color
	 * @return colorized text
	 */
	public SpannedText colorize(String subString, String color) {
		int start = spannable.toString().indexOf(subString);
		spannable.setSpan(new ForegroundColorSpan(Color.parseColor(color)), start, start + subString.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
		return this;
	}

	/**
	 * 
	 * @param start
	 *            -start of substring
	 * @param end
	 *            - end of substring
	 * @param color
	 *            - color
	 * @return colorized string
	 */
	public SpannedText colorizeAt(int start, int end, String color) {
		spannable.setSpan(new ForegroundColorSpan(Color.parseColor(color)), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
		return this;
	}

}
